package com.ruoyi.classai.controller;


import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.classai.domain.dto.resource.EwcTeachingResourceDto;
import com.ruoyi.classai.domain.dto.resource.EwcTeachingResourceSaveDto;
import com.ruoyi.classai.domain.dto.resource.EwcTeachingResourceSingleSaveDto;
import com.ruoyi.classai.domain.vo.resource.EwcTeachingResourceAnalyseDetailVo;
import com.ruoyi.classai.domain.vo.resource.EwcTeachingResourceVo;
import com.ruoyi.classai.service.IEwcChapterResourceService;
import com.ruoyi.classai.service.IEwcTeachingResourceService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;


/**
 * 教学视频解析对象Controller
 *
 * @author ZSP
 * @date 2025-06-07
 */
@RestController
@RequestMapping("/api/teachingResource")
public class EwcTeachingResourceController extends BaseController {

    @Autowired
    private IEwcTeachingResourceService iEwcTeachingResourceService;

    @Autowired
    private IEwcChapterResourceService iEwcChapterResourceService;

    /**
     * 视频分析-新增视频解析
     */
    @Operation(summary = "视频分析-新增视频解析")
    @PostMapping(value = "/addResource")
    public AjaxResult addResource(@RequestBody @Validated EwcTeachingResourceDto ewcTeachingResourceDto) {
        String id = String.valueOf(iEwcTeachingResourceService.addResource(ewcTeachingResourceDto));
        if(!id.isEmpty()){
            JSONObject resultJson = new JSONObject();
            resultJson.put("id", id);
            return AjaxResult.success("提交成功！",resultJson);
        }
        return AjaxResult.error("提交失败！");
    }

    /**
     * 视频解析资源列表
     */
    @Operation(summary = "查询视频解析资源列表")
    @GetMapping("/getTeachingResourceList")
    public TableDataInfo getTeachingResourceList(String resourceName, Integer subjectType, String deadlineStart, String deadlineEnd) {
        startPage();
        List<EwcTeachingResourceVo> list = iEwcTeachingResourceService.getTeachingResourceList(resourceName,subjectType,deadlineStart,deadlineEnd);
        return getDataTable(list);
    }

    /**
     * 删除视频解析资源
     */
    @Operation(summary = "删除视频解析资源")
    @GetMapping("/deleteResource")
    public AjaxResult deleteResource(@NotBlank(message = "视频解析资源ID不能为空") @RequestParam String id) {
        boolean result = iEwcTeachingResourceService.deleteResource(id);
        if(result){
            boolean resultCR = iEwcChapterResourceService.updateTeachingResourceStatus(id);
        }
        if(result){
            return AjaxResult.success("提交成功！");
        }
        return AjaxResult.error("提交失败！");
    }

    /**
     * 视频解析资源解析详情
     */
    @Operation(summary = "查询视频解析资源解析详情")
    @GetMapping("/getTeachingResourceAnalyseDetail")
    public AjaxResult getTeachingResourceAnalyseDetail(@NotBlank(message = "视频解析资源ID不能为空") @RequestParam  String id) {
        EwcTeachingResourceAnalyseDetailVo teachingResourceAnalyseDetail = iEwcTeachingResourceService.getTeachingResourceAnalyseDetail(id);
        JSONObject resultJson = new JSONObject();
        resultJson.put("teachingResourceAnalyseDetail", teachingResourceAnalyseDetail);
        return AjaxResult.success("查询成功", resultJson);
    }

    /**
     * 视频分析-保存资源
     */
    @Operation(summary = "视频分析-保存资源")
    @PostMapping(value = "/saveTeachingResource")
    public AjaxResult saveTeachingResource(@RequestBody @Validated EwcTeachingResourceSaveDto ewcTeachingResourceSaveDto) {
        EwcTeachingResourceAnalyseDetailVo teachingResourceAnalyseDetail = iEwcTeachingResourceService.getTeachingResourceAnalyseDetail(ewcTeachingResourceSaveDto.getId());
        if(!Objects.isNull(teachingResourceAnalyseDetail)
                && ewcTeachingResourceSaveDto.getTaskId().equals(teachingResourceAnalyseDetail.getTaskId()) ){
            AjaxResult result = iEwcChapterResourceService.saveTeachingResource(ewcTeachingResourceSaveDto, teachingResourceAnalyseDetail);
            if(result.isSuccess()){
                return AjaxResult.success("提交成功！");
            }
        }
        return AjaxResult.error("提交数据异常！");
    }

    /**
     * 我的资源库-保存资源
     */
    @Operation(summary = "我的资源库-保存资源")
    @PostMapping(value = "/singleSaveTeachingResource")
    public AjaxResult singleSaveTeachingResource(@RequestBody @Validated EwcTeachingResourceSingleSaveDto ETSingleSaveDto) {
        boolean result = iEwcChapterResourceService.SingleSaveTeachingResource(ETSingleSaveDto);
        if(result){
            return AjaxResult.success("提交成功！");
        }
        return AjaxResult.error("提交数据异常！");
    }

}
